var module = angular.module('app.GoodsController', []);

module.controller('GoodsController',function ($scope, ConfigModule, $http, $location) {
    //$location.path('/order/index');
    //$scope.hideNav = true;

});


module.controller('GoodsInfoController',function ($scope, API,$stateParams,addCart,$state,$ionicSlideBoxDelegate,$timeout,$ionicPopup) {
    //$location.path('/order/index');
    $scope.$emit('hideNav',true);
    API.getGoodsDetail({
        _id : $stateParams.goods_id
    },function(err,result){
        if(result.status != 200){
            return $ionicHistory.goBack();
        }
        $scope.alert = function(msg){
            var confirm = $ionicPopup.confirm({
                title: '商品信息',
                template : msg,
                buttons: [
                    {
                        text: '<b>确定</b>',
                        type: 'button-positive'
                    }
                ]
            });
            confirm.then(function(){
                $state.go('index');
            });
        };
        if(result.data.GoodsDetail.total <= 0){
            $scope.alert('商品库存不足！');
        }

        $scope.goods = result.data.GoodsDetail;
        $ionicSlideBoxDelegate.$getByHandle('goods.imgs').update();
        $ionicSlideBoxDelegate.$getByHandle('goods.imgs').loop(true);
        $timeout(
            function() {
                uParse('#content', {
                    rootPath: '../'})
            },
            500
        );
    });
    $scope.$emit('hideNav',true);
    $scope.pay = function(){
        addCart.set([{
            goods_id : $stateParams.goods_id,
            goods_number : 1,
            goods_price : $scope.goods.price,
            goods_thumb : $scope.goods.thumb,
            goods_title : $scope.goods.title,
        }]);
        $state.go('order.submit');
    };
    $scope.addcart = function(){
        var data = [];
        data[$stateParams.goods_id] = 1;
        addCart.set(data);
        $state.go('goods.cart');
    }
});


module.controller('GoodsCartController',function ($scope, API,addCart,$state,$ionicPopup) {
    var data = addCart.get();
    if(!isEmptyObject(data)){
        for(var i in data){
            API.addToCart({
                goods_id : i,
                number : data[i]
            },function(err,result){
                $scope.list = result.data.goods_info;
                $scope.total_price = result.data.total_price;
                for(var i in $scope.list){
                    $scope.list[i].checked = 'ion-ios-checkmark-outline';
                    if($scope.list[i].goods_number <= 0){
                        $scope.delete_goods($scope.list[i].goods_id);
                    }
                }
            });
        }
    }else{
        API.getCartList(function(err,result){
            $scope.list = result.data.goods_info;
            $scope.total_price = result.data.total_price;
            for(var i in $scope.list){
               $scope.list[i].checked = 'ion-ios-checkmark-outline';
                if($scope.list[i].goods_number <= 0){
                    $scope.delete_goods($scope.list[i].goods_id);
                }
            }
        });
    }

    $scope.$emit('hideNav',true);
    angular.forEach($scope.list,function(item){
        item.checked = 'ion-ios-checkmark-outline';
    });
    $scope.checkgoods = function(){
        var checked = true;
        this.item.checked = (this.item.checked == 'ion-ios-checkmark-outline')?'ion-ios-circle-outline':'ion-ios-checkmark-outline';
        for(var i in $scope.list){
            if($scope.list[i].checked == 'ion-ios-circle-outline'){
                checked = false;
                $scope.select_status = 'ion-ios-circle-outline';
            }
        }
        if(checked){
            $scope.select_status = 'ion-ios-checkmark-outline';
        }
        prices();

        //angular.forEach($scope.list,function(item){
        //    if(item.checked == 'ion-ios-circle-outline'){
        //    }
        //});
    };
    $scope.select_status = 'ion-ios-checkmark-outline';
    $scope.selectall = function(){
        $scope.select_status = ($scope.select_status == 'ion-ios-checkmark-outline')?'ion-ios-circle-outline':'ion-ios-checkmark-outline';
        angular.forEach($scope.list,function(item){
            item.checked = ($scope.select_status == 'ion-ios-checkmark-outline')?'ion-ios-checkmark-outline':'ion-ios-circle-outline';
            prices();
        });
    };
    $scope.plusnum = function(){
        this.item.goods_number = (this.item.goods_number)?this.item.goods_number+1:1;
        API.updateCartGoods({
            goods_id : this.item.goods_id,
            number : 1,
            operate : 1
        },function(err,result){
            $scope.list = result.data.goods_info;
            for(var i in $scope.list){
                $scope.list[i].checked = 'ion-ios-checkmark-outline';
            }
            prices();
        });
    };
    $scope.minusnum = function(){
        this.item.goods_number = (this.item.goods_number > 0)?this.item.goods_number-1:0;
        API.updateCartGoods({
            goods_id : this.item.goods_id,
            number : 1,
            operate : 2
        },function(err,result){
            $scope.list = result.data.goods_info;
            for(var i in $scope.list){
                $scope.list[i].checked = 'ion-ios-checkmark-outline';
            }
        });
        if(this.item.goods_number == 0){
            $scope.delete_goods(this.item.goods_id);
        }
    };
    $scope.$watch('list',function(newv,oldv,scope){
        prices();
    });
    $scope.delete_goods = function(goods_id){
        API.deleteGoods({
            goods_id : goods_id
        },function(err,result){
            $scope.list = result.data.goods_info;
            for(var i in $scope.list){
                $scope.list[i].checked = 'ion-ios-checkmark-outline';
            }
            prices();
        });
    }

    $scope.cancel = function() {
        var cancels = $ionicPopup.confirm({
            title: '消息提示',
            template: '请选择商品!',
            buttons: [
                {
                    text: '<b>确定</b>',
                    type: 'button-positive'
                },
            ]
        });
    };
    $scope.submit = function(){
        var data = [];
        for(var key in $scope.list){
            if($scope.list[key].checked == "ion-ios-checkmark-outline"){
                // var goods = {
                //     goods_id : $scope.list[key].goods_id,
                //     goods_number : $scope.list[key].goods_number
                // };
                //data[$scope.list[key].goods_id] = $scope.list[key].goods_number;
                data.push($scope.list[key]);
            }
        }
        // data = JSON.stringify(data);
        if(!data[0]){
            $scope.cancel();
        }else{
            addCart.set(data);
            $state.go('order.submit');
        }
    };
    function prices(){
        var newv = $scope.list;
        var price = 0;
        for(x in newv){
            if(newv[x].checked == "ion-ios-checkmark-outline"){
                price += parseFloat(newv[x].goods_price) * parseFloat(newv[x].goods_number);
            }
        }
        $scope.total_price = parseFloat(price).toFixed(2);
    }

});


module.controller('GoodsIndexController',function ($scope, API,$stateParams,$location,addCart,$ionicPopup,$ionicSlideBoxDelegate) {

    if( !$stateParams.cate_id ){
        return $state.go('index');
    }
    API.getSignAds({
        sign : 'category_banner'
    },function(err,result){
        $scope.SignAds = result.data.SignAds;
        $ionicSlideBoxDelegate.$getByHandle('SignAds').update();
        $ionicSlideBoxDelegate.$getByHandle('SignAds').loop(true);
    });
    $scope.total_price = 0.00;
    $scope.cate_id = $stateParams.cate_id;
    API.getCateList({
        sign_shop : 'xykr'
    },function(err,result){
        $scope.category = result;
    });
    API.getCateGoodsList({
        pcate : $scope.cate_id,
        pagesize : 100
    },function(err,result){
        $scope.goods_list = result.data.CateGoodsList;
    });

    $scope.myActiveSlide = 1;
    $scope.up = 'ion-ios-arrow-down';
    $scope.$emit('hideNav',true);
    $scope.select = function(){
        $scope.up = ($scope.showSelect)?'ion-ios-arrow-down':'ion-ios-arrow-up';
        $scope.showSelect = ($scope.showSelect)?false:true;
    };
    $scope.hideSelect = function(){
        $scope.showSelect = false;
    };
    $scope.select_child = function(cate_id){

        API.getCateList({
            pcate : cate_id
        },function(err,result){

            if(result[0]){
                $scope.category_child = result;
            }else{
                API.getCateGoodsList({
                    pcate : cate_id,
                    pagesize : 100
                },function(err,result){
                    $scope.goods_num = 0;
                    $scope.cate_id = cate_id;
                    $scope.goods_list = result.data.CateGoodsList;
                });
                $scope.showSelect = false;
            }
        });
    };
    $scope.select_category_child = function(cate_id){
        $scope.selected = this.x;
        $scope.showSelect = false;
        $scope.up = 'ion-ios-arrow-down';

        API.getCateGoodsList({
            pcate : cate_id,
            pagesize : 100
        },function(err,result){
            $scope.goods_num = 0;
            $scope.cate_id = cate_id;
            $scope.goods_list = result.data.CateGoodsList;
        });
        $scope.showSelect = false;
    };
    $scope.goods_num = 0;
    $scope.plusnum = function(){
        $scope.total_price = parseFloat($scope.total_price) + parseFloat(this.item.price);
        $scope.total_price = $scope.total_price.toFixed(2);
        $scope.goods_num++;
        this.item.num = (this.item.num)?this.item.num+1:1;
    };
    $scope.minusnum = function(){
        $scope.total_price = parseFloat($scope.total_price) - parseFloat(this.item.price);
        $scope.total_price = $scope.total_price.toFixed(2);
        $scope.goods_num--;
        this.item.num = (this.item.num > 0)?this.item.num-1:0;
    };
    var direction;
    $scope.check = function(order){
        $scope.showSelect = false;
        //if($scope.)
        if(order == 'clicks'){
            if($scope.clicks == 1){
                $scope.clicks = -1;
                direction = -1;
                $scope.clicks_class = 'ion-ios-arrow-up';
                $scope.sale_num = false;
            }else{
                $scope.clicks = 1;
                direction = 1;
                $scope.sale_num = false;
                $scope.clicks_class = 'ion-ios-arrow-down';
            }
        }else if(order == 'sale_num'){
            if($scope.sale_num == 1){
                $scope.sale_num = -1;
                direction = -1;
                $scope.clicks = false;
                $scope.sale_num_class = 'ion-ios-arrow-up';

            }else{
                $scope.sale_num = 1;
                direction = 1;
                $scope.clicks = false;
                $scope.sale_num_class = 'ion-ios-arrow-down';
            }
        }else{
            $scope.clicks = false;
            $scope.sale_num = false;
            direction = '';
        }
        API.getCateGoodsList({
            pcate : $scope.cate_id,
            sort : order,
            direction : direction,
            pagesize : 100
        },function(err,result){
            $scope.goods_num = 0;
            $scope.goods_list = result.data.CateGoodsList;
        });
    };

    $scope.submit = function(){
        var data = {};
        for(var key in $scope.goods_list){
            if($scope.goods_list[key].num > 0){
                data[$scope.goods_list[key].goods_id] = $scope.goods_list[key].num;
            }
        }
        $scope.showAlert = function() {
            var alertPopup = $ionicPopup.alert({
                title: '提示',
                template: '您还没有选择商品!'
            });
        };
        if(isEmptyObject(data)){
            $scope.showAlert();
        }else{
            addCart.set(data);
            $location.path('/goods/cart');
        }
        //console.log(data);
        //for(var i=0;i<=$scope.goods_list.length;i++){
        //    console.log($scope.goods_list[i]);
        //}
    };
});

var isEmptyObject = function( obj ) {

    for ( var name in obj ) {
        return false;
    }
    return true;
};




